package com.ptl.bp;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson2.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class SimpleExcelReader {

    public static void main(String[] args) {
        String filePath = "D:/workspace/ptl-battery-passport/BpBackend/src/test/resources/数据模板.xlsx";

        List<BatteryData> syncData = EasyExcel.read(filePath)
                .head(BatteryData.class)
                .sheet()
                .doReadSync();

        System.out.println("同步读取到 " + syncData.size() + " 条数据:");
        trans(syncData, 0);
        trans(syncData, 1);

        //
        Map<String, String> map = new LinkedHashMap<>();
        Map<String, String> map2 = new LinkedHashMap<>();
        for (BatteryData data : syncData) {
            String[] arr = data.getCategory().split("\n");
            String[] subArr = data.getSubCategory().split("\n");
            String[] attrArr = data.getDataAttribute().split("\n");
            String[] accessArr = data.getDataAccess().split("\n");
            String[] recommendArr = data.getRecommended().split("\n");

            map.put(String.format("%s_%s_%s", arr[1], subArr[1], attrArr[1]), accessArr[1]);
            map2.put(String.format("%s_%s_%s", arr[1], subArr[1], attrArr[1]), data.getUnit());
        }
        System.out.println(JSON.toJSONString(map));
        System.out.println(JSON.toJSONString(map2));

    }

    private static void trans(List<BatteryData> syncData, int i1) {
        Map<String, String> map = new LinkedHashMap<>();

        // 数据处理示例
        for (int i = 0; i < syncData.size(); i++) {
            BatteryData data = syncData.get(i);
            String[] arr = data.getCategory().split("\n");
            map.put(arr[1], arr[i1]);

            String[] subArr = data.getSubCategory().split("\n");
            map.put(subArr[1], subArr[i1]);

            String[] attrArr = data.getDataAttribute().split("\n");
            map.put(attrArr[1], attrArr[i1]);

            String[] descArr = data.getDescription().split("\n");
            if (descArr.length > 1) {
                map.put(descArr[1], descArr[i1]);
            }
        }
        System.out.println(JSON.toJSONString(map));
    }

    @Data
    public static class BatteryData {
        @ExcelProperty(index = 0)
        private String category;

        @ExcelProperty(index = 1)
        private String subCategory;

        @ExcelProperty(index = 2)
        private String dataAttribute;

        @ExcelProperty(index = 3)
        private String description;

        @ExcelProperty(index = 4)
        private String dataAccess;

        @ExcelProperty(index = 5)
        private String recommended;

        @ExcelProperty(index = 6)
        private String unit;

        @ExcelProperty(index = 7)
        private String staticDynamic;

        @ExcelProperty(index = 8)
        private String informationLevel;
    }
}